#include "imagemodel.h"
#include <QDebug>
ImageModel::ImageModel(ImageManager *manager,QObject *parent) :
    QAbstractTableModel(parent), m_manager(manager)
{
}


int ImageModel::rowCount(const QModelIndex & parent) const{

    return m_manager->GetCount();
}

int ImageModel::columnCount(const QModelIndex & parent) const{
    return 5;
}

QVariant ImageModel::data ( const QModelIndex & index, int role) const{
    int row = index.row();
    AED_Image *img = m_manager->GetImage(row);
    if(role==Qt::DisplayRole||role==Qt::EditRole){

        QVariant var;

        if(index.column()==1)
            var = img->GetName();
        else if(index.column()==2)
            var = img->GetXDiv();
        else if(index.column()==3)
            var = img->GetYDiv();
        else if(index.column()==4)
            var = img->GetPath();
        else
            return QVariant(QVariant::Invalid);
        return var;
    }else if(role==Qt::DecorationRole){
        if(index.column()==0)
            return img->ToPixmap().scaledToHeight(64,Qt::SmoothTransformation);
    }
    return QVariant(QVariant::Invalid);
}

QVariant ImageModel::headerData ( int section, Qt::Orientation orientation, int role) const{
    if(orientation==Qt::Horizontal){
        if (role==Qt::DisplayRole||role==Qt::EditRole){
            if(section==0)
                return "Preview";
            if(section==1)
                return "Name";
            if(section==2)
                return "X Div";
            if(section==3)
                return "Y Div";
            if(section==4)
                return "Path";
            else
                return QVariant(QVariant::Invalid);
        }else{
            return QVariant(QVariant::Invalid);
        }
    }else{
        if (role==Qt::DisplayRole||role==Qt::EditRole){
            QVariant h(QVariant::Int);
            h=section+1;
            return h;
        }else{
            return QVariant(QVariant::Invalid);
        }
    }
    return QVariant(QVariant::Invalid);
}

bool ImageModel::setData(const QModelIndex &index, const QVariant &value, int role){
    int row = index.row();
    int column = index.column();
    AED_Image *img = m_manager->GetImage(row);
    if(index.isValid() && role==Qt::EditRole){
        if(column==2){
            int x_div = value.toInt();
            if(x_div>0)
                img->SetXDiv(x_div);
        }else if(column==3){
            int y_div = value.toInt();
            if(y_div>0)
                img->SetYDiv(y_div);
        }else{
            return false;
        }
        emit dataChanged(index,index);
        return true;
    }

    return false;
}

Qt::ItemFlags ImageModel::flags(const QModelIndex &index) const{
    Qt::ItemFlags flags;

    if(index.column()==0||index.column()==1)
        flags = Qt::ItemIsEnabled|Qt::ItemIsSelectable;
    else
        flags = Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable;

    return flags;
}

int ImageModel::LoadImageK(const QString &path){

    if(!m_manager->LoadImageK(path))
        return 0;

    emit layoutChanged();
    return 1;

}

bool ImageModel::Load(QDomElement &header, QString &scene_path){

    if(!m_manager->Load(header,scene_path))
        return 0;

    emit layoutChanged();
    return 1;
}

AED_Image* ImageModel::GetImage(int index){
    return m_manager->GetImage(index);


}

void ImageModel::EraseImage(int index){

    m_manager->EraseImage(m_manager->GetImage(index)->GetPath());
    emit layoutChanged();
}

void ImageModel::Update(){
    emit layoutChanged();
}
